package com.jlcon.nas.controller;

import java.io.File;
import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jlcon.nas.biz.aria.AriaClient;
import com.jlcon.nas.biz.aria.dto.Aria2ActiveTask;
import com.jlcon.nas.config.NasConfig;
import com.jlcon.nas.utils.CommonDataResponse;
import com.jlcon.nas.utils.CommonResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("api/file")
@Tag(name = "测试")
@RequiredArgsConstructor
public class FileTestController {

	private final NasConfig nasConfig;
	private final AriaClient aria2Client;
	
	@PostMapping(value = "test")
	@Operation(summary = "访问本地文件测试")
	public CommonResponse commonDataResponseResult() {
		var file = new File(nasConfig.getSourcePath());
		if (file.isDirectory()) {
			StringBuilder sb = new StringBuilder();
			for (File mp4 : file.listFiles()) {
				sb.append(mp4.getName());
				sb.append(" ");
			}
			return CommonResponse.buildSuccessResult(sb.toString());
		} else {
			return CommonResponse.buildFailureResult(nasConfig.getSourcePath() + "不是目录！");
		}
    }
	
	@PostMapping(value = "download-test")
	@Operation(summary = "下载文件测试")
	public CommonResponse testDownload(String magnet) throws Throwable {
		String gid = aria2Client.addUri(magnet);
		return CommonResponse.buildSuccessResult(gid);
	}
	
	@PostMapping(value = "tell-active")
	@Operation(summary = "查看所有活动任务")
	public CommonDataResponse<List <Aria2ActiveTask>> tellActive(@RequestBody String...keys) throws Throwable {
		return CommonDataResponse.buildSuccessResult(aria2Client.tellActive(keys));
	}
	
//	@GetMapping(value = "force-remove")
//	@Operation(summary = "删除任务")
//	public CommonResponse forceRemove(String gid) throws Throwable {
//		return CommonResponse.buildSuccessResult(aria2Client.forceRemove(gid));
//	}
}